package sto.orz.server;

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.NCSARequestLog;
import org.mortbay.jetty.Request;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.AbstractHandler;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.handler.RequestLogHandler;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.security.HashUserRealm;
import org.mortbay.jetty.security.UserRealm;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.thread.BoundedThreadPool;
import org.mortbay.xml.XmlConfiguration;

public class Jetty {
	public static void main(String[] args) throws Exception {
		// Server server = new Server();
		// XmlConfiguration configuration = new XmlConfiguration(
		// new FileInputStream("myJetty.xml"));
		// configuration.configure(server);
		// server.start();
		Server server = new Server();

		BoundedThreadPool threadPool = new BoundedThreadPool();
		threadPool.setMaxThreads(100);
		server.setThreadPool(threadPool);

		Connector connector = new SelectChannelConnector();
		connector.setPort(8080);
		server.setConnectors(new Connector[] { connector });

		HandlerCollection handlers = new HandlerCollection();

		ContextHandlerCollection contexts = new ContextHandlerCollection();
		// ...................
		WebAppContext context = new WebAppContext();
		context.setContextPath("/rptps3");
		context.setResourceBase("./dist/webapps/rptps3");
		context.setParentLoaderPriority(false);

		contexts.setHandlers(new Handler[] { context });
		// ...................
		RequestLogHandler requestLogHandler = new RequestLogHandler();
		handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(),
				requestLogHandler });
		server.setHandler(handlers);

		// TODO add javadoc context to contexts

		// WebAppContext.addWebApplications(server, "./webapps",
		// "org/mortbay/jetty/webapp/webdefault.xml", true, false);

		// HashUserRealm userRealm = new HashUserRealm();
		// userRealm.setName("Test Realm");
		// userRealm.setConfig("./etc/realm.properties");
		// server.setUserRealms(new UserRealm[] { userRealm });

		NCSARequestLog requestLog = new NCSARequestLog(
				"./logs/jetty-yyyy-mm-dd.log");
		requestLog.setExtended(false);
		requestLogHandler.setRequestLog(requestLog);

		server.setStopAtShutdown(true);
		server.setSendServerVersion(true);

		server.start();
		server.join();
	}

	public static void Hello() throws Exception {
		Handler handler = new AbstractHandler() {
			public void handle(String target, HttpServletRequest request,
					HttpServletResponse response, int dispatch)
					throws IOException, ServletException {
				response.setContentType("text/html");
				response.setStatus(HttpServletResponse.SC_OK);
				response.getWriter().println("<h1>Hello</h1>");
				((Request) request).setHandled(true);
			}
		};

		Server server = new Server(8080);
		server.setHandler(handler);
		server.start();
	}
}
